﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk.Workflow;
using System.Activities;
using Microsoft.Xrm.Sdk;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Crm.Sdk;

/**
 * Copyright 2011 by Alberto Gemin
 * agemin@hotmail.com
 * Version 1.7.0.1
 * 13 Dec 2011
 **/
namespace Gemina.CRM2011.WF.LdgManager
{
    [AgCodeActivity(ActivityNameResetUserFilters, AgViewsUtils.GroupName)]
    public class AgResetUserFilters : AgCodeActivityBase
    {

        #region Constants

        private const string ActivityNameResetUserFilters = "Reset User Filters";
        private const string Param1InputUserToReset = "User to Reset";
        private const string Param2InputResetOfflineFilters = "Reset Offline Filters?";
        private const string Param3InputResetOutlookFilters = "Reset Outlook Filters?";
        private const string MsgNull = "{0} cannot be null.";
        #endregion

        #region Workflow Parameters

        /// <summary>
        /// Input parameter: User to be blanked.
        /// </summary>
        [Input(Param1InputUserToReset)]
        [ReferenceTarget(AgViewsUtils.EntityNameSystemUser)]
        [RequiredArgument]
        public InArgument<EntityReference> InputUserToReset { get; set; }

        /// <summary>
        /// Input parameter: Reset Outlook Offline Filters
        /// </summary>
        [Input(Param2InputResetOfflineFilters)]
        [Default(AgViewsUtils.ConstTrue)]
        public InArgument<bool> InputResetOfflineFilters { get; set; }

        /// <summary>
        /// Input parameter: Reset Outlook Online Filters
        /// </summary>
        [Input(Param3InputResetOutlookFilters)]
        [Default(AgViewsUtils.ConstTrue)]
        public InArgument<bool> InputResetOutlookFilters { get; set; }

        #endregion

        #region CodeActivity

        override protected bool ExecuteBody(CodeActivityContext executionContext)
        {
            // impersonate
            IOrganizationService impersonatedSvc = this.GetService(executionContext, this.InputUserToReset.Get(executionContext).Id);
            ResetUserFiltersRequest req = new ResetUserFiltersRequest();

            if (this.InputUserToReset.Get(executionContext) == null)
            {
                throw new AgErrorMessage(String.Format(MsgNull, Param1InputUserToReset));
            }

            if (this.InputResetOfflineFilters.Get(executionContext))
            {
                req.QueryType = SavedQueryQueryType.OfflineFilters;
                ResetUserFiltersResponse resp = (ResetUserFiltersResponse)impersonatedSvc.Execute(req);
            }

            if (this.InputResetOutlookFilters.Get(executionContext))
            {
                req.QueryType = SavedQueryQueryType.OutlookFilters;
                ResetUserFiltersResponse resp = (ResetUserFiltersResponse)impersonatedSvc.Execute(req);
            }

            return true;
        }

        #endregion

    }
}
